package com.fzy.core;

import com.fzy.core.entity.MappingClass;
import com.fzy.core.entity.MappingProperty;

public class MappingClassBuilder {
	MappingBuilder parent;
	MappingClass mappingClass;

	public MappingClassBuilder(MappingBuilder mappingBuilder,
			MappingClass mappingClass) {
		super();
		this.parent = mappingBuilder;
		this.mappingClass = mappingClass;
	}

	public MappingBuilder endClass() {
		parent.mappings.put(mappingClass.getName(), mappingClass);
		return parent;
	}

	private MappingClassBuilder _addProperty(MappingProperty mappingProperty) {
		mappingProperty.setParentClass(mappingClass);
		mappingClass.getProperties().add(mappingProperty);
		return this;
	}

	public MappingClassBuilder property(String name, String show,
			String column, String comment) {
		MappingProperty p = new MappingProperty(name, show, column, comment);
		return _addProperty(p);
	}

	public MappingClassBuilder property(String name, String show,
			String column, String comment, boolean isQuery, boolean isNullAvailable) {
		MappingProperty p = new MappingProperty(name, show, column, comment);
		p.setNullAvailable(isNullAvailable);
		p.setQuery(isQuery);
		return _addProperty(p);
	}

	public MappingClassBuilder property(String name, String showName,
			String column, String comment, int type, int length,
			boolean isNullAvailable, String defaultValue,
			boolean isForeign, String foreignClass, boolean isQuery) {
		MappingProperty p = new MappingProperty(name, showName, column,
				comment, type, length, isNullAvailable, defaultValue, false,
				isForeign, foreignClass, isQuery, true, null);
		return _addProperty(p);
	}

	public MappingClassBuilder property(String name, String showName,
			String column, String comment, int type, int length,
			boolean isNullAvailable, String defaultValue, boolean isId,
			boolean isForeign, String foreignClass, boolean isQuery,
			boolean isShow) {
		MappingProperty p = new MappingProperty(name, showName, column,
				comment, type, length, isNullAvailable, defaultValue, isId,
				isForeign, foreignClass, isQuery, isShow, null);
		return _addProperty(p);
	}

	public MappingClassBuilder id() {
		String seq = "s_" + mappingClass.getTable();
		MappingProperty p = new MappingProperty("id", "ID", "id", "ID(" + seq
				+ ")");
		p.setId(true);
		p.setType(MappingProperty.LONG);
		p.setNullAvailable(false);
		p.setShow(false);
		return _addProperty(p);
	}

	public MappingClassBuilder status() {
		MappingProperty p = new MappingProperty("status", "状态", "status", "记录状态(0,无效,1,有效)");
		p.setLength(1);
		p.setNullAvailable(false);
		p.setShow(false);
		p.setDefaultValue("1");
		return _addProperty(p);
	}

	public MappingClassBuilder record() {
		MappingProperty p = new MappingProperty("createdDate", "创建时间", "created_date", "记录创建时间");
		p.setType(MappingProperty.DATE);
		p.setNullAvailable(false);
		p.setShow(false);
		p.setParentClass(mappingClass);
		mappingClass.getProperties().add(p);
		
		p = new MappingProperty("createdUser", "创建人", "created_user", "记录创建人(User.id)");
		p.setType(MappingProperty.LONG);
		p.setNullAvailable(false);
		p.setShow(false);
		p.setParentClass(mappingClass);
		mappingClass.getProperties().add(p);
		
		p = new MappingProperty("updatedDate", "更新时间", "updated_date", "记录更新时间");
		p.setType(MappingProperty.DATE);
		p.setShow(false);
		p.setParentClass(mappingClass);
		mappingClass.getProperties().add(p);
		
		p = new MappingProperty("updatedUser", "更新人", "updated_user", "记录更新人(User.id)");
		p.setType(MappingProperty.LONG);
		p.setShow(false);
		p.setParentClass(mappingClass);
		mappingClass.getProperties().add(p);
		return this;
	}
}
